package org.darkforest.common.services.impl;

import org.darkforest.common.services.GoodsCommonService;
import org.darkforest.common.services.PXianshiGoodsService;
import org.darkforest.dao.mapper.GoodsMapper;
import org.darkforest.dao.mapper.PXianshiGoodsMapper;
import org.darkforest.dao.mapper.PXianshiMapper;
import org.darkforest.model.*;
import org.darkforest.vo.AjaxReturnEntity;
import org.darkforest.vo.XianShiGoodsPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Created by Libing on 2015/6/20.
 */
@Service
@Transactional
public class PXianshiGoodsServiceImpl implements PXianshiGoodsService {
    @Autowired
    private PXianshiGoodsMapper pXianshiGoodsMapper;

    @Autowired
    private PXianshiMapper xianshiMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsCommonService goodsCommonService;

    @Override
    public List<PXianshiGoods> findPXianshiGoodsListByCondition(PXianshiGoods pXianshiGoods) {
        return pXianshiGoodsMapper.findPXianshiGoodsListByCondition(pXianshiGoods);
    }

    @Override
    public AjaxReturnEntity<PXianshiGoods> doAddDiscountGoods(Integer goods_id, Integer xianshi_id, float xianshi_price, String xinshi_jingo) {
        AjaxReturnEntity<PXianshiGoods> pXianshiGoodsAjaxReturnEntity = new AjaxReturnEntity<PXianshiGoods>();
        PXianshiGoods pxianshigoods = new PXianshiGoods();
        pxianshigoods.setGoodsId(goods_id);
        pxianshigoods.setXianshiId(xianshi_id);
        List<PXianshiGoods> pxianshigoodsList= this.pXianshiGoodsMapper.findPXianshiGoodsListByCondition(pxianshigoods);

        if(pxianshigoodsList.size()==0) {
            try {
                //根据商品ID查询goods表信息
                Goods goods = goodsMapper.selectById(goods_id);

                PXianshi pXianshi = xianshiMapper.selectByPrimaryKey(xianshi_id);
                PXianshiGoods pXianshiGoods = new PXianshiGoods();
                pXianshiGoods.setGoodsName(goods.getGoodsName());
                pXianshiGoods.setEndTime(pXianshi.getEndTime());
                pXianshiGoods.setGoodsId(goods.getGoodsId());
                pXianshiGoods.setGoodsImage(goods.getGoodsImage());
                pXianshiGoods.setGoodsPrice(goods.getGoodsPrice());
                pXianshiGoods.setLowerLimit(pXianshi.getLowerLimit());
                pXianshiGoods.setStartTime(pXianshi.getStartTime());
                pXianshiGoods.setState(pXianshi.getState());
                pXianshiGoods.setXianshiExplain(pXianshi.getXianshiExplain());
                pXianshiGoods.setStoreId(pXianshi.getStoreId());
                pXianshiGoods.setXianshiTitle(pXianshi.getXianshiTitle());
                pXianshiGoods.setXianshiId(pXianshi.getXianshiId());
                pXianshiGoods.setXianshiName(pXianshi.getXianshiName());
                pXianshiGoods.setXianshiPrice(new BigDecimal(xianshi_price));
                pXianshiGoods.setXianShiGoodsslogan(xinshi_jingo);
                pXianshiGoods.setGoodsMarketprice(goods.getGoodsMarketprice());
                pXianshiGoods.setGoodsCommonid(goods.getGoodsCommonid());
                pXianshiGoodsMapper.insertSelective(pXianshiGoods); //添加限时特卖商品


                //限时特卖商品添加完成之后 修改商品的价格为限时特卖价格 复制整条数据形成一条新的商品记录
//                goods.setGoodsPrice(new BigDecimal(xianshi_price));
//                goods.setGoodsPromotionPrice(new BigDecimal(xianshi_price).divide(goods.getGoodsPrice()));
//                goods.setGoodsPromotionType((byte) 2);
//                goods.setGoodsType(1);
//                goodsMapper.updateByPrimaryKeySelective(goods);
                //根据主键查询goods_common查询信息
//                GoodsCommonWithBLOBs gc = goodsCommonService.selectById(goods.getGoodsCommonid());
//                gc.setGoodsCommonid(null);
//                gc.setGoodsType(1);
//                gc.setGoodsPrice(new BigDecimal(xianshi_price));
//                gc.setSpecName("N;");
//                gc.setSpecValue("N;");
//                if(goods.getGoodsSpec()!=""&&!goods.getGoodsSpec().equals("N;")){
//                    gc.setGoodsName(gc.getGoodsName()+"-"+goods.getGoodsSpec());}
//                goodsCommonService.insertBySelective(gc);
//                goodsMapper.insert(goods1);









                pXianshiGoodsAjaxReturnEntity.setDatas(pXianshiGoods);
                pXianshiGoodsAjaxReturnEntity.setCode("true");
                pXianshiGoodsAjaxReturnEntity.setMessage("限时折扣添加商品成功");
            } catch (Exception e) {

                pXianshiGoodsAjaxReturnEntity.setCode("false");
                pXianshiGoodsAjaxReturnEntity.setMessage("限时折扣添加商品失败");
                e.printStackTrace();

            }
            return pXianshiGoodsAjaxReturnEntity;
        }else{
            pXianshiGoodsAjaxReturnEntity.setCode("false");
            pXianshiGoodsAjaxReturnEntity.setMessage("改限时折扣活动已经添加此商品");
            return pXianshiGoodsAjaxReturnEntity;
        }


    }

    /**
     * 删除特卖商品时 需要将商品的价格修改为原来的售价
     * @param xianshi_goods_id
     * @return
     */
    public AjaxReturnEntity<PXianshiGoods> deleteDiscountGoods(Integer xianshi_goods_id) {
        //删除特卖商品时 需要将商品的价格修改为原来的售价
//        PXianshiGoods pXianshiGoods =pXianshiGoodsMapper.selectByPrimaryKey(xianshi_goods_id);
//
//        Goods goods = new Goods();
//        goods.setGoodsId(pXianshiGoods.getGoodsId());
//        goods.setGoodsPrice(pXianshiGoods.getGoodsPrice());
//        goodsMapper.updateByPrimaryKeySelective(goods);



        pXianshiGoodsMapper.deleteByPrimaryKey(xianshi_goods_id);
        AjaxReturnEntity<PXianshiGoods> ajaxReturnEntity = new AjaxReturnEntity<PXianshiGoods>();
        ajaxReturnEntity.setCode("true");
        ajaxReturnEntity.setMessage("限时折扣活动商品删除成功");
        return ajaxReturnEntity;
    }

    /**
     *   修改特卖商品价格后 需要同步更新商品售价
     * @param xianshi_goods_id
     * @param xianshi_price
     * @param goods_jingo
     * @return
     */
    public AjaxReturnEntity<PXianshiGoods> editDiscountGoods(Integer xianshi_goods_id, float xianshi_price, String goods_jingo) {

        PXianshiGoods pxianshigoods = pXianshiGoodsMapper.selectByPrimaryKey(xianshi_goods_id);

//        if(new BigDecimal(xianshi_price).compareTo(pxianshigoods.getXianshiPrice())!=0){
//            Goods goods = new Goods();
//            goods.setGoodsPrice(new BigDecimal(xianshi_price));
//            goods.setGoodsId(pxianshigoods.getGoodsId());
//            goodsMapper.updateByPrimaryKeySelective(goods);
//        }

        pxianshigoods.setXianshiGoodsId(xianshi_goods_id);
        pxianshigoods.setXianShiGoodsslogan(goods_jingo);
        pxianshigoods.setXianshiPrice(new BigDecimal(xianshi_price));
        pXianshiGoodsMapper.updateByPrimaryKeySelective(pxianshigoods);













        AjaxReturnEntity<PXianshiGoods> ajaxReturnEntity = new AjaxReturnEntity<PXianshiGoods>();
        ajaxReturnEntity.setDatas(pxianshigoods);
        ajaxReturnEntity.setCode("true");
        ajaxReturnEntity.setMessage("限时折扣活动商品修改成功");
        return ajaxReturnEntity;
    }

    /**
     * 商品限时折扣宣传图
     *
     * @param pxianshiGoodsId
     * @param xianshiId
     * @param activeImage
     */
    @Override
    public void modifyPXianShiGoodsToUploadImage(String pxianshiGoodsId, String xianshiId, String activeImage) {
        PXianshiGoods pXianshiGoods = new PXianshiGoods();
//        pXianshiGoods.setXianshiId(Integer.parseInt(xianshiId));
        pXianshiGoods.setXianShiGoodsImage(activeImage);
        pXianshiGoods.setXianshiGoodsId(Integer.parseInt(pxianshiGoodsId));
        pXianshiGoodsMapper.updateByPrimaryKeySelective(pXianshiGoods);
    }

    @Override
    public XianShiGoodsPage getXianshiGoodsPage(XianShiGoodsPage page) {
        PXianshiGoods goods = new PXianshiGoods();
        page.setStart(1);
        page.setPageSize(8);
        page.setResultList(findPXianshiGoodsListByCondition(goods));
        return page;
    }

    @Override
    public void updatePXianshiStatePriceBatch(Map<String, Object> map) {
        this.pXianshiGoodsMapper.updatePXianshiStatePriceBatch(map);
    }

    /**
     * 批量获取特卖商品根据特卖活动ID
     *
     * @param map
     * @return
     */
    @Override
    public List<PXianshiGoods> batchFindPXianshiGoods(Map<String, Object> map) {
        return this.pXianshiGoodsMapper.batchFindPXianshiGoods(map);
    }
}
